package a66weding.com.jiehuntong.fragment.homeshopcase;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.bumptech.glide.Glide;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.home.HomeShopCaseActivity;
import a66weding.com.jiehuntong.activity.home.hotel.shop.HomeProductdetailActivity;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.StoreDetailGuess;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.common.MyViewHolder;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/9/3
 * @description :
 */
public class ShopAllNewFragment extends BaseShopCaseFragment implements OnNetResultListener {


    private HomeShopCaseActivity mShopCaseactivity;

    private XRefreshView xRefreshView;
    private GridView mGridView;
    private TextView mNomoreTv;


    private int casesCount;
    private int businessID;
    private List<StoreDetailGuess.DataBean> shopDataList;
    private CommonAdapter<StoreDetailGuess.DataBean> mAdapter;


    private int pagerIndex=1;
    private boolean isDownRefresh;
    private boolean isPullLoadMore;

    @Override
    public void getData() {
        mShopCaseactivity = (HomeShopCaseActivity) getActivity();
        casesCount = mShopCaseactivity.getCasesCount();
        businessID = mShopCaseactivity.getBusinessID();

        getNetData(1);

    }

    @Override
    public View initView(LayoutInflater inflater, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.item_shopdetail_casefragment, null);
        xRefreshView = (XRefreshView) contentView.findViewById(R.id.detail_leftgrid_xrefreshview);
        mGridView = (GridView) contentView.findViewById(R.id.gridview_detail_leftitem);
        mNomoreTv = (TextView) contentView.findViewById(R.id.tv_nomore);

        initRefreshView();

        return contentView;
    }


    @Override
    public void initData() {

        if(shopDataList!=null) {
            mAdapter= new CommonAdapter<StoreDetailGuess.DataBean>(mShopCaseactivity,shopDataList,R.layout.item_home_detail_content_griditem){

                @Override
                public void convert(MyViewHolder holder, int position) {
                    final StoreDetailGuess.DataBean dataBean = shopDataList.get(position);
                    String nvc_picMain = dataBean.getNvc_PicMain();
                    ImageView imgVIew = (ImageView) holder.getView(R.id.iv_gridlable);
                    Glide.with(mShopCaseactivity).load(nvc_picMain).into(imgVIew);

                    holder.setOnClickListener(R.id.ll_home_left_pager, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int i_id = dataBean.getI_Id();
                            Intent intent = new Intent(mShopCaseactivity, HomeProductdetailActivity.class);
                            intent.putExtra("productId",i_id);
                            mShopCaseactivity.startActivity(intent);
                            mShopCaseactivity.finish();

                        }
                    });
                    holder.setText(R.id.tv_gridname,dataBean.getNvc_Name());
                    holder.setText(R.id.tv_gridcollection,dataBean.getI_Collections()+"人收藏");
                    holder.setText(R.id.tv_shopprice,"￥" + dataBean.getDm_ShopPrice());
                    TextView priceTv = (TextView) holder.getView(R.id.tv_price);
                    priceTv.setText("￥" + dataBean.getDm_Price());
                    priceTv.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
                }
            };

            mGridView.setAdapter(mAdapter);
        }



    }


    /**
     * GET WeddingMobileApi/get_ProductListBusinessJ30?Token={Token}&pageIndex={pageIndex}&businessID={businessID}
     最近发布 30天以内的商品
     * @param pagindex
     */
    public  void  getNetData(int pagindex){
        //&pageIndex={pageIndex}&businessID={businessID}
        String url =  RequestCode.get_ProductListBusinessJ30+"&BusinessID="+businessID+"&pageIndex="+pagindex;
        HttpRequestBean bottomListRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,url,this, StoreDetailGuess.class,RequestCode.GET_PRODUCTLIST);
        HttpHandleUtil.executeCommonGet(mShopCaseactivity,bottomListRequestBean);
    }


    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof StoreDetailGuess) {
            if (isDownRefresh){
                xRefreshView.stopRefresh();
            }else{
                xRefreshView.stopLoadMore();
            }

            StoreDetailGuess shopData= (StoreDetailGuess) data;
            switch (shopData.getResCode()) {


                case "0000":
                        List<StoreDetailGuess.DataBean> dataList = shopData.getData();
                        if(pagerIndex==1) {
                            shopDataList = dataList;
                            pagerIndex++;
                            initData();

                        }else if(pagerIndex>1){
                            shopDataList.addAll(dataList);

                            if(mAdapter!=null) {
                                mAdapter.notifyDataSetChanged();
                            }
                            pagerIndex++;

                        }



                    break;
                     case "0003":
                         mNomoreTv.setVisibility(View.VISIBLE);
                    break;
            }


            initData();


        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        CUtils.showDataErrToast(mShopCaseactivity);

    }


    //初始化上啦刷新
    private void initRefreshView() {
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(false);

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh() {
                isDownRefresh = true;
                isPullLoadMore=false;
                pagerIndex=1;
                getNetData(1);

            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isPullLoadMore = true;
                isDownRefresh = false;
                getNetData(pagerIndex);
            }
        });
    }
}
